package com.good.child.http;

import static com.good.child.config.RequestID.Community.ADD_COMMUNITY_LIST;

import android.content.Context;
import android.text.TextUtils;

import com.good.child.R;
import com.good.child.bean.BaseResponse;
import com.good.child.bean.ai.CommunityBean;
import com.good.child.bean.constants.ConstantsBean;
import com.good.child.config.Api;
import com.good.child.config.RequestID;
import com.good.child.ui.utils.CommonU;
import com.lzy.okgo.model.Response;
import com.yimidida.library.base.ICallBackResultListener;
import com.yimidida.library.http.BaseReqBean;
import com.yimidida.library.http.LoadingBaseObserver;
import com.yimidida.library.tool.JsonSerializer;
import com.yimidida.library.utils.LogUtils;
import com.yimidida.library.utils.ToastUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Create on: 2024/8/21
 * by Mr.Zhu
 * Description: 社区
 */
public class CommunityLogical extends AppBaseLogical{

    private static CommunityLogical instance;

    public static CommunityLogical getInstance() {
        if (instance == null) {
            synchronized (CommunityLogical.class) {
                if (instance == null) {
                    instance = new CommunityLogical();
                }
            }
        }
        return instance;
    }

    /**
     * 发布
     * @param context
     * @param mSelectList
     * @param content
     * @param resultListener
     */
    public void requestConstants(Context context, List<String> mSelectList,String content, ICallBackResultListener resultListener) {
        String url = Api.Community.URL_ADD;
        requestFileForPost(url, mSelectList,content, new CommunityObserver(context, RequestID.Community.ADD_ID, resultListener));
    }

    /**
     * 社区列表
     * @param context
     * @param resultListener
     */
    public void requestCommunityList(Context context,String userId,ICallBackResultListener resultListener) {
        String url = Api.Community.URL_COMMUNITY_LIST;
        Map<String,String> map=new HashMap<>();
//        map.put("userId",userId);
        requestStringForGet(url,map,new CommunityObserver(context, ADD_COMMUNITY_LIST, resultListener));
    }



    private class CommunityObserver extends LoadingBaseObserver {
        private ICallBackResultListener resultListener;
        private int requestID;

        public CommunityObserver(Context activity, int requestID, ICallBackResultListener resultListener) {
            this.resultListener = resultListener;
            this.requestID = requestID;
            setShowLoading(activity);
        }

        @Override
        public void onNext(Response<String> stringResponse) {
            super.onNext(stringResponse);
            if (stringResponse == null || stringResponse.getRawResponse().code() != SUCCESS_CODE || TextUtils.isEmpty(stringResponse.body())) {
                ToastUtil.showLong(R.string.data_fail);
                return;
            }
            String bodyString = stringResponse.body();
            try {
                switch (requestID) {
                    case RequestID.Community.ADD_ID:
                        LogUtils.showLog("发表社区 ：" + bodyString);
                        BaseResponse bean = JsonSerializer.deSerialize(bodyString, BaseResponse.class);
                        if (bean.code == 200) {
                            if (resultListener != null) {
                                resultListener.onCallBack(true);
                            }
                        } else {
                            ToastUtil.showLong(bean.msg);
                            if (resultListener != null) {
                                resultListener.onCallBack(false);
                            }
                        }
                        break;
                    case RequestID.Community.ADD_COMMUNITY_LIST:
                        LogUtils.showLog("社区列表 ：" + bodyString);
                        CommunityBean communityBean = JsonSerializer.deSerialize(bodyString, CommunityBean.class);
                        if (communityBean.code == 200) {
                            if (resultListener != null) {
                                resultListener.onCallBack(communityBean);
                            }
                        } else {
                            ToastUtil.showLong(communityBean.msg);
                            if (resultListener != null) {
                                resultListener.onCallBack(null);
                            }
                        }
                        break;

                }
            } catch (Exception e) {
                ToastUtil.showLong(R.string.data_exception);
                if (resultListener != null) {
                    resultListener.onCallBack(null);
                }
            }

        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            ToastUtil.showLong(R.string.data_error);
            if (resultListener != null) {
                resultListener.onCallBack(null);
            }
        }
    }
}
